package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.AppManagement;
import com.turing.post.cardmanagement.util.AppManagementDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * @author zxp
 * @Apr 9, 2013
 */
@Stateless(mappedName = "cardmanagement.impl.AppManagementDaoImpl")
@Remote
@Local
public class AppManagementDaoImpl implements AppManagementDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public AppManagement getAppManagementByAppId(BigInteger appId) {
		return _em.find(AppManagement.class, appId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppManagement> getAppManagementList(Integer page) {
		if (page.intValue() == -1) {
			String sql = "select * from AppManagement t order by appId";
			Query query = _em.createNativeQuery(sql, AppManagement.class);
			return query.getResultList();
		} else {
			int maxResult = Config.PAGE_SIZE;
			int startPosition = maxResult * page;
			String sql = "select * from AppManagement t order by appId";
			Query query = _em.createNativeQuery(sql, AppManagement.class)
					.setFirstResult(startPosition).setMaxResults(maxResult);
			return query.getResultList();
		}
	}

	@Override
	public Integer getAppManagementTotal() {
		String sql = "select nvl(count(*),0) from AppManagement";
		Query query = _em.createNativeQuery(sql);
		return Integer.parseInt((query.getSingleResult().toString()));
	}

	@Override
	public void insert(AppManagement amg) throws PCPSApplicationException {
		GeneralDao.insert(_em, amg);
	}

	@Override
	public void update(AppManagement amg) throws PCPSApplicationException {
		GeneralDao.update(_em, amg);
	}

	@SuppressWarnings("unchecked")
	@Override
	public AppManagement getAppManagementByAID(String AID) {
		String sql = "select * from AppManagement t where t.AID=?1";
		Query query = _em.createNativeQuery(sql, AppManagement.class);
		query.setParameter(1, AID);
		List<AppManagement> ctList = query.getResultList();
		if (ctList != null && ctList.size() > 0) {
			return ctList.get(0);
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppManagement> getAppManageListByAppIds(String appids) {
		if(PCPSUtil.isEmpty(appids)){
			return null;
		}
		String appidsSql=" where t.appid in (null";
		String[] appidStr = appids.split(",");
		for (int i = 0; i < appidStr.length; i++) {
			appidsSql += ","+appidStr[i];
		}
		appidsSql +=") ";
		String sql = "select * from AppManagement t "+appidsSql+" order by appId";
		Query query = _em.createNativeQuery(sql, AppManagement.class);
		return query.getResultList();
	}
}
